using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MMM.Core.Domain;
using ServiceStack.ServiceHost;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using MMM.Services.Base;
using MMM.Services.Operations;
using MMM.Services.Type;
using MMM.Services.Response;
using System.Collections;
using ServiceStack.ServiceInterface;
using MMM.Core;

namespace MMM.Services
{
    class ArgentPEqualityComparer : IEqualityComparer<AgentProduct>
    {

        public bool Equals(AgentProduct x, AgentProduct y)
        {
            return x.CityCode == y.CityCode;
        }

        public int GetHashCode(AgentProduct obj)
        {
            return obj.CityCode.GetHashCode();
        }
    }

    public class AgentProductTreeNodeService : RestServiceBase<AgentProductTreeNode>
    {
        IRepository<AgentProduct> proRep = ApplicationContext.Container.GetInstance<IRepository<AgentProduct>>();
        public override object OnGet(AgentProductTreeNode request)
        {
            var node = new AgentProductTreeNode { City = "委托城市" };
            AddCityNode(node, node.CityCode);
            return node;
        }

        private void AddCityNode(AgentProductTreeNode node, string cityCode)
        {
            
            var lst=proRep.GetAll();

            var productList = lst.Distinct(new ArgentPEqualityComparer()).ToList().OrderBy(t => t.CityCode);            
            if (productList.Count() > 0)
            {
                foreach (var item in productList)
                {
                    var sNode = new AgentProductTreeNode { expanded = true, iconCls = "", leaf = true, CityCode = item.CityCode, City = item.City };
                    node.children.Add(sNode);
                }
            }
        }
    }

    public class AgentProductService2 : BaseRestService<AgentProductDto>
    {
        public override object OnGet(AgentProductDto request)
        {
            var repAgentProduct = this.GetRepository<AgentProduct>();
            var proRep = this.GetRepository<ProductInfo>();

            // 委托方代理产品
            var lstAgentP = repAgentProduct.GetAll(t => t.CityCode == request.CityCode && t.AgentId == request.AgentId);

            // 委托方所在城市的所有产品
            var lst = proRep.GetAll(t => t.AreaCode == request.CityCode);

            IList<AgentProductDto> lstDto = new List<AgentProductDto>();

            foreach (var item in lst)
            {
                var s = lstAgentP.FirstOrDefault(t => t.ProductId == item.Id);
                AgentProductDto dto;
                if (s == null)
                {
                    // 未委托
                    dto = new AgentProductDto
                    {
                        //Id=Guid.NewGuid(),
                        AgentId = request.AgentId,
                        AgentName = request.AgentName,
                        City = item.AreaName,
                        CityCode = item.AreaCode,
                        IsChecked = false,
                        Product = item.ProductName,
                        ProductId = item.Id
                    };
                }
                else
                {
                    // 已委托
                    // dto = s as AgentProductDto;
                    dto = new AgentProductDto
                    {
                        AgentId = s.AgentId,
                        AgentName = s.AgentName,
                        City = s.City,
                        CityCode = s.CityCode,
                        IsChecked = true,
                        Product = s.Product,
                        ProductId = s.ProductId,
                        Price = s.Price,
                        Id = s.Id
                    };

                }
                lstDto.Add(dto);
            }
            return new OperationResult<AgentProductDto>(lstDto, lstDto.Count);
        }
    }

    public class AgentProductListService : BaseRestService<AgentProductList>
    {
        public override object OnPost(AgentProductList request)
        {
            if (request != null && request.Count > 0)
            {
                var rep = GetRepository<AgentProduct>();
                var addList = new List<AgentProduct>();
                var editList = new List<AgentProduct>();

                foreach (var item in request)
                {
                    if (item.Id.HasValue)
                    {
                        var agentProductEdit = item.ToAgentProduct();
                        if (item.IsChecked)
                        {
                            editList.Add(agentProductEdit);
                        }
                        else
                        {
                            rep.DeleteByID(item.Id);
                        }
                    }
                    else
                    {
                        if (item.IsChecked)
                        {
                            var agentProductAdd = item.ToAgentProduct();
                            agentProductAdd.Id = Guid.NewGuid();
                            addList.Add(agentProductAdd);
                        }
                    }
                }
                if (addList.Count > 0)
                {
                    rep.Add(addList);
                }
                if (editList.Count > 0)
                {
                    rep.Update(editList);
                }

            }
            return new OperationResult<AgentProductList>().success;
        }

    }
    /// <summary>
    /// 委托产品管理 服务类(GET)
    /// </summary>
    public class AgentProductGetSerice : BaseGetService<AgentProductPageGet, AgentProduct>
    {
        //protected override System.Linq.Expressions.Expression<Func<AgentProduct, bool>> CreateFilter(AgentProductPageGet request)
        //{
        //    return base.CreateFilter(request);
        //}
    }
    /// <summary>
    /// 委托产品管理 服务类(DELETE)
    /// </summary>
    public class AgentProductDeleteService : BaseDeleteService<AgentProduct, AgentProductDelete>
    {
    }
}
